/**
 * 
 */
package com.nari.basic.action;

import java.util.Arrays;
import java.util.List;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.basic.model.IMenu;
import com.nari.basic.service.IMenuService;
import com.nari.basic.service.IRoleMenuService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.coreframe.webframe.SysEnvironmentMgr;
import com.nari.platform.components.dhtmltree.DhtmlTreeNode;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.WebConstants;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-7-18
 */
public class MenuAction extends JSONActionSupport {
	private static final long serialVersionUID = -7976727646401560144L;
	private IMenuService iMenuService;
	private IRoleMenuService iRoleMenuService;
	private Boolean ifQueryAuthorityMenu = true; //标识是否查询权限菜单，默认查询权限菜单
	private String menuID;
	private List<IMenu> menuList;
	private String menuName;
	//菜单项管理
	private IMenu menu;
	private String parentMenuID;
	//角色菜单管理
	private String roleUID;
	private String menuIDList;
	
	/**
	 * 菜单信息管理导航
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-26
	 */
	public String queryMenuMgrNav() throws Exception {
		//便于携带系统参数
		return "menuMgrNav";
	}
	
	/**
	 * 查询根菜单，并将其转化为树节点
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-23
	 */
	@SuppressWarnings("unchecked")
	public String queryRootMenuTree() throws Exception {
		//构造动态树虚拟头节点
		DhtmlTreeNode rootNode = new DhtmlTreeNode();
		rootNode.setId("0");
		rootNode.setText("0");
		//根据标识信息确定是否查询登录用户权限菜单
		List<IMenu> authorityMenuList = null;
		if(ifQueryAuthorityMenu!=null && this.ifQueryAuthorityMenu) {
			authorityMenuList = (List<IMenu>) SysEnvironmentMgr.getSessionInstance().getAttribute(WebConstants.USER_MENUS);
		}
		List<IMenu> rootMenuList = this.iMenuService.queryFirstLevelMenu(authorityMenuList);
		//构造菜单树节点
		List<DhtmlTreeNode> childNodes = this.iMenuService.formatMenuListToTreeNodes(rootMenuList, null);
		rootNode.getItem().addAll(childNodes);
		this.writeJsonStrToResponse(JSONUtil.serialize(rootNode));
		return Action.SUCCESS;
	}
	
	/**
	 * 查询子菜单，并将其转化为树节点
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-23
	 */
	@SuppressWarnings("unchecked")
	public String queryChildMenuTree() throws Exception {
		//查询父菜单
		IMenu pMenu = this.iMenuService.queryEntityById(menuID);
		//根据标识信息确定是否查询登录用户权限菜单
		List<IMenu> authorityMenuList = null;
		if(ifQueryAuthorityMenu!=null && this.ifQueryAuthorityMenu) {
			authorityMenuList = (List<IMenu>) SysEnvironmentMgr.getSessionInstance().getAttribute(WebConstants.USER_MENUS);
		}
		List<IMenu> childMenuList = this.iMenuService.queryChildMenuByParent(menuID, authorityMenuList);
		//构造父菜单树节点
		DhtmlTreeNode rootNode = null;
		if(childMenuList!=null && childMenuList.size()>0) {
			//非叶子节点
			rootNode = this.iMenuService.formatMenuListToTreeNodes(Arrays.asList(new IMenu[]{pMenu}), false).get(0);
		} else {
			//叶子节点
			rootNode = this.iMenuService.formatMenuListToTreeNodes(Arrays.asList(new IMenu[]{pMenu}), true).get(0);
		}
		//构造子菜单树节点
		List<DhtmlTreeNode> childNodes = this.iMenuService.formatMenuListToTreeNodes(childMenuList, null);
		rootNode.getItem().clear();
		rootNode.getItem().addAll(childNodes);
		this.writeJsonStrToResponse(JSONUtil.serialize(rootNode));
		return Action.SUCCESS;
	}
	
	/**
	 * 查询系统菜单列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-24
	 */
	public String queryAllMenuList() throws Exception {
		this.menuList = this.iMenuService.queryAllMenusInOrder();
		return "menuList";
	}
	
	/**
	 * 通过菜单名称查询菜单列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-24
	 */
	public String queryByMenuName() throws Exception {
		this.menuList = this.iMenuService.queryByMenuNameLike(menuName);
		return "menuList";
	}
	
	/**
	 * 菜单项管理
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-24
	 */
	public String queryMenuForManage() throws Exception {
		if(this.menuID!=null && !"".equals(menuID)) {
			this.menu = this.iMenuService.queryEntityById(menuID);
		}
		return "menuManage";
	}
	
	/**
	 * 新增或修改菜单项
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-24
	 */
	public String saveOrUpdateMenu() throws Exception {
		this.rmsg = this.iMenuService.saveOrUpdateMenuItem(menu);
		this.writeJsonStrToResponse(JSONUtil.serialize(this.rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 通过父菜单ID查询子菜单列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-24
	 */
	public String queryByParentMenuID() throws Exception {
		this.menuList = this.iMenuService.queryChildMenuByParent(parentMenuID, null);
		return "menuList";
	}
	
	/**
	 * 菜单删除
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-24
	 */
	public String deleteMenuByID() throws Exception {
		//判断是否存在子菜单
		int childrenCounter = this.iMenuService.countChildrenByPMenuID(menuID);
		if(childrenCounter > 0) {
			this.rmsg = ResponseMsg.getInstance(false, "由于存在子菜单，该菜单无法被删除！");
		} else {
			this.iMenuService.deleteEntityById(menuID);
			this.iRoleMenuService.deleteByMenuID(menuID);
			this.rmsg = ResponseMsg.getInstance(true, "");
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(this.rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 角色菜单授权初始化
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-29
	 */
	public String initRoleMenuMgr() throws Exception {
		//角色唯一标识roleUID初始化
		return "roleMenuMgr";
	}
	
	/**
	 * 查询角色授权的菜单树
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-30
	 */
	public String queryAuthorityMenuTree() throws Exception {
		DhtmlTreeNode authorityTree = this.iMenuService.queryAuthorityTreeByRole(roleUID, parentMenuID);
		this.writeJsonStrToResponse(JSONUtil.serialize(authorityTree));
		return Action.SUCCESS;
	}
	
	/**
	 * 保存角色菜单授权信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-30
	 */
	public String saveRoleMenuAuthority() throws Exception {
		this.rmsg = this.iMenuService.saveRoleMenuAuthority(roleUID, menuIDList);
		this.writeJsonStrToResponse(JSONUtil.serialize(this.rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * @return the iRoleMenuService
	 */
	@JSON(serialize=false)
	public IRoleMenuService getiRoleMenuService() {
		return iRoleMenuService;
	}

	/**
	 * @param iRoleMenuService the iRoleMenuService to set
	 */
	public void setiRoleMenuService(IRoleMenuService iRoleMenuService) {
		this.iRoleMenuService = iRoleMenuService;
	}

	/**
	 * @return the iMenuService
	 */
	@JSON(serialize=false)
	public IMenuService getiMenuService() {
		return iMenuService;
	}
	
	/**
	 * @param iMenuService the iMenuService to set
	 */
	public void setiMenuService(IMenuService iMenuService) {
		this.iMenuService = iMenuService;
	}

	/**
	 * @return the ifQueryAuthorityMenu
	 */
	public Boolean getIfQueryAuthorityMenu() {
		return ifQueryAuthorityMenu;
	}

	/**
	 * @param ifQueryAuthorityMenu the ifQueryAuthorityMenu to set
	 */
	public void setIfQueryAuthorityMenu(Boolean ifQueryAuthorityMenu) {
		this.ifQueryAuthorityMenu = ifQueryAuthorityMenu;
	}

	/**
	 * @return the menuID
	 */
	public String getMenuID() {
		return menuID;
	}

	/**
	 * @param menuID the menuID to set
	 */
	public void setMenuID(String menuID) {
		this.menuID = menuID;
	}

	/**
	 * @return the menuList
	 */
	public List<IMenu> getMenuList() {
		return menuList;
	}

	/**
	 * @param menuList the menuList to set
	 */
	public void setMenuList(List<IMenu> menuList) {
		this.menuList = menuList;
	}

	/**
	 * @return the menuName
	 */
	public String getMenuName() {
		return menuName;
	}

	/**
	 * @param menuName the menuName to set
	 */
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	/**
	 * @return the menu
	 */
	public IMenu getMenu() {
		return menu;
	}

	/**
	 * @param menu the menu to set
	 */
	public void setMenu(IMenu menu) {
		this.menu = menu;
	}

	/**
	 * @return the parentMenuID
	 */
	public String getParentMenuID() {
		return parentMenuID;
	}

	/**
	 * @param parentMenuID the parentMenuID to set
	 */
	public void setParentMenuID(String parentMenuID) {
		this.parentMenuID = parentMenuID;
	}

	/**
	 * @return the roleUID
	 */
	public String getRoleUID() {
		return roleUID;
	}

	/**
	 * @param roleUID the roleUID to set
	 */
	public void setRoleUID(String roleUID) {
		this.roleUID = roleUID;
	}

	/**
	 * @return the menuIDList
	 */
	public String getMenuIDList() {
		return menuIDList;
	}

	/**
	 * @param menuIDList the menuIDList to set
	 */
	public void setMenuIDList(String menuIDList) {
		this.menuIDList = menuIDList;
	}

}
